package com.highper.gateway.controller;

import com.highper.common.entity.response.HighperResponse;
import com.highper.gateway.service.IDynamicRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @Author ZhangGJ
 * @Date 2022/02/10 17:34
 */
@RestController
@RequestMapping("dynamic/route")
public class DynamicRouteController {

    private final IDynamicRouteService dynamicRouteService;

    @Autowired
    public DynamicRouteController(IDynamicRouteService dynamicRouteService) {
        this.dynamicRouteService = dynamicRouteService;
    }

    @GetMapping("list")
    public Flux<RouteDefinition> list() {
        return dynamicRouteService.list();
    }

    @PostMapping("insert")
    public HighperResponse insert(@RequestBody RouteDefinition routeDefinition) {
        return HighperResponse.resultWebFlux(dynamicRouteService.insert(routeDefinition));
    }

    @PostMapping("update")
    public HighperResponse update(@RequestBody RouteDefinition routeDefinition) {
        return HighperResponse.resultWebFlux(dynamicRouteService.update(routeDefinition));
    }

    @DeleteMapping("delete")
    public Mono<ResponseEntity<Object>> delete(String id) {
        return dynamicRouteService.delete(id);
    }

}
